<!-- 01否决指标法弹框 -->
<template>
  <a-drawer
    width="40%"
    :visible="props.visible"
    @ok="handleOk"
    @cancel="handleCancel"
    draggable
    title-align="center"
    unmountOnClose
  >
    <template #title>
      {{ props.data?.id ? '编辑指标' : '新增指标' }}
    </template>
    <div>
      <a-form
        :model="formData.data"
        ref="refForm"
        :rules="rules"
        auto-label-width
      >
        <a-form-item label="指标名称" field="name" show-colon>
          <a-input v-model="formData.data.name"></a-input>
        </a-form-item>
        <a-form-item label="指标定义" field="definition" show-colon>
          <a-textarea
            v-model="formData.data.definition"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="项目界定" field="define" show-colon>
          <a-textarea
            v-model="formData.data.define"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="评价标准/规则" field="rule" show-colon>
          <a-textarea
            v-model="formData.data.rule"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="考核目标值" field="goal" show-colon>
          <a-tooltip
            content="
注：加和计算评分规则下，每一项指标最高评分上限不得超过总分上限，
且各项指标得分和，要小于等于总分上限。
加权计算评分规则下，每一项指标的评分上限默认等于总分上限。"
          >
            <icon-info-circle-fill style="color: #165dff; font-size: 16px" />
          </a-tooltip>
          <a-input-number
            :disabled="toolStore.getCalculate === 2"
            v-model="formData.data.goal"
            hide-button
          >
            <template #suffix>
              <div>分</div>
            </template>
          </a-input-number>
        </a-form-item>
        <a-form-item label="评分方式" field="markWay" show-colon>
          <a-radio-group v-model="formData.data.markWay">
            <a-radio :value="1">直接输入</a-radio>
            <a-radio :value="2">公式计算</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="评分人" field="personnel" show-colon>
          <!-- 系统自动评分 -->
          <a-tooltip
            content="注：评分人在平常的情况下，都是默认关闭，只有特殊指定的模板，才
开启，并且一直会指定某个指定的人员进行评分。
平常关闭状态下是以每个单独评价中”的设置定的评分人为准"
          >
            <icon-info-circle-fill style="color: #165dff; font-size: 16px" />
          </a-tooltip>
          <a-switch
            v-model="formData.data.personnel"
            :checked-value="1"
            :unchecked-value="2"
          ></a-switch>
        </a-form-item>
        <a-form-item label="备注" field="remark" show-colon>
          <a-textarea
            v-model="formData.data.remark"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="新增到指标库" field="" show-colon>
          <a-radio-group v-model="formData.isAddStore">
            <a-radio :value="true">是</a-radio>
            <a-radio :value="false">否</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
      <!-- <CountSetting v-model:visible="countSettingVisiable"></CountSetting> -->
    </div>
    <template #footer>
      <a-space>
        <a-button @click="handleCancel">取消</a-button>
        <a-button type="primary" @click="handleOk">确认</a-button>
        <a-button v-if="isShowContinueBtn" type="primary" @click="continueAdd"
          >确认并继续新增</a-button
        >
      </a-space>
    </template>
  </a-drawer>
</template>

<script lang="ts" setup>
import { cloneDeep } from 'lodash';
import { useAssessmentToolStore } from '@/store';
import CountSetting from '../CountSetting/index.vue';

const toolStore = useAssessmentToolStore();

const props = withDefaults(
  defineProps<{
    visible: boolean; // 是否显示对话框
    data?: any; // 传进来的数据,
  }>(),
  {}
);

const emits = defineEmits(['update:visible', 'extraIndexEmit']);
let refForm = ref();
let countSettingVisiable = ref(false); // 是否设置计算公式弹出框
let isShowContinueBtn = ref(true); // 是否显示继续新增按钮

const formData = reactive<any>({
  data: {
    name: '', // 指标名称
    definition: '', // 指标定义
    define: '', // 项目界定
    rule: '', // 评分标准/规则
    dataSource: undefined, // 数据来源，评分人
    // dimensionaId: undefined, // 纬度id
    goal: undefined, // 考核目标值
    personnel: 2, // 评分人 1开启 0关闭
    remark: '', // 备注
    isAddStore: false // 是否新增到指标库
  }
});

const originData = {
  name: '', // 指标名称
  definition: '', // 指标定义
  define: '', // 项目界定
  rule: '', // 评分标准/规则
  dataSource: undefined, // 数据来源，评分人
  // dimensionaId: undefined, // 纬度id
  personnel: 2, // 评分人 1开启 0关闭
  goal: undefined, // 考核目标值
  remark: '', // 备注
  isAddStore: false // 是否新增到指标库
};

const rules = {
  name: [
    {
      required: true,
      message: '请输入'
    }
  ],
  rule: [
    {
      required: true,
      message: '请输入'
    }
  ],
  goal: [
    {
      required: true,
      message: '请输入'
    }
  ]
};

watch(
  () => props.visible,
  (newVal: boolean) => {
    if (newVal) {
      if (props.data) {
        formData.data = cloneDeep(props.data);
        isShowContinueBtn.value = false;
      }
      formData.data.goal =
        toolStore.getCalculate === 2 ? toolStore.getUpperScore : undefined;
    } else {
      isShowContinueBtn.value = true;
      formData.data = cloneDeep(originData);
    }
  }
);

// 加和计算tips表格数据
const sumData = [
  {
    name: '指标1',
    score: '评分1'
  },
  {
    name: '指标2',
    score: '评分2'
  },
  {
    name: '指标3',
    score: '评分3'
  }
];

// 加权计算tips表格数据
const weightData = [
  {
    name: '指标1',
    weight: '50%',
    score: '评分1'
  },
  {
    name: '指标2',
    weight: '30%',
    score: '评分2'
  },
  {
    name: '指标3',
    weight: '20%',
    score: '评分3'
  }
];

const handleOk = async () => {
  if (await refForm.value.validate()) {
    return;
  }
  emits('extraIndexEmit', cloneDeep(formData.data));
  emits('update:visible', false);
};

const continueAdd = async () => {
  if (await refForm.value.validate()) {
    return;
  }
  emits('extraIndexEmit', cloneDeep(formData.data), true);
  emits('update:visible', true);
  formData.data = cloneDeep(originData);
  formData.data.goal =
    toolStore.getCalculate === 2 ? toolStore.getUpperScore : undefined;
};

const handleCancel = () => {
  emits('update:visible', false);
};
</script>

<style scoped lang="less">
.icon {
  cursor: pointer;
  font-size: 20px;
}
</style>
